/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.service.impl;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import java.util.Date;
import java.util.List;
import org.crystalrange.dao.TransactionDao;
import org.crystalrange.model.Transaction;
import org.crystalrange.model.User;
import org.crystalrange.service.TransactionService;
import org.crystalrange.service.UserService;

public class TransactionServiceImpl implements TransactionService {

    private TransactionDao transactionDao;
    private UserService userService;

    public void setTransactionDao(TransactionDao transactionDao) {
        this.transactionDao = transactionDao;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public Transaction create(String username) {
        
        User currentUser = this.userService.findByUsername(username); 
        
        Transaction transaction = new Transaction();
        transaction.setCreatedBy(currentUser);
        transaction.setCreatedDate(new Date());
        
        return transaction;
    }
    
    public boolean save(Transaction transaction, String username) {
        
        User currentUser = this.userService.findByUsername(username); 
        
        transaction.setUpdatedBy(currentUser);
        transaction.setUpdatedDate(new Date());
        
        return this.transactionDao.save(transaction);
    }

    public boolean remove(Transaction transaction) {
        return this.transactionDao.remove(transaction);
    }

    public Transaction find(Long id) {
        return this.transactionDao.find(id);
    }

    public Transaction findByCode(String code) {
        
        Search searchTransaction = new Search(Transaction.class);
        searchTransaction.addFilterEqual("code", code);
        
        return this.transactionDao.searchUnique(searchTransaction);
    }
    
    public List<Transaction> findAll() {
        return this.transactionDao.findAll();
    }

    public SearchResult<Transaction> searchAndCount(Search search) {
        return this.transactionDao.searchAndCount(search);
    }
    
    public List<Transaction> search(Search search) {
        return this.transactionDao.search(search);
    }
    
    public Transaction searchUnique(Search search) {
        return this.transactionDao.searchUnique(search);
    }
}
